<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:doc="livcos.org/ns/doc"
	xmlns:cv="feurerworks.com/ns/hr/cv"
  xmlns:ad="livcos.org/ns/admin"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:html="http://www.w3.org/1999/xhtml"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:v="livcos.org/ns/view"
  exclude-result-prefixes="doc cv ad f xs html v">

	<xsl:import href="../../../../livcos.org/ns/admin/location.xsl"/>
	
	<xsl:template match="cv:particulars">
  	<xsl:variable name="p" select="f:entity(@ref)"/>
    <div id="{@id}">
    	<v:style ref="{resolve-uri('styles.css')}"/>
    	<table class="cv cv-particulars">
      	<tbody>
      		<tr>
      			<td class="cv-label"><xsl:copy-of select="cv:name/node()"/></td>
      			<td>
      				<xsl:value-of select="$p/ad:first-name"/>
      				<xsl:text> </xsl:text>
      				<xsl:value-of select="$p/ad:name"/>
      			</td>
      			<td class="cv-label"><xsl:copy-of select="cv:birthday/node()"/></td>
      			<td>
							<xsl:value-of
								select="format-date(xs:date($p/ad:birthday), '[D] [MNn] [Y]', /*/@language, (), /*/@country)"
							/></td>
      		</tr>
      		<tr>
      			<td class="cv-label"><xsl:copy-of select="cv:address/node()"/></td>
      			<td><xsl:apply-templates select="$p/ad:address[@type = 'main']" mode="cv"/></td>
      			<td class="cv-label"><xsl:copy-of select="cv:native/node()"/></td>
      			<td><xsl:apply-templates select="$p/ad:address[@type = 'native']" mode="cv"/></td>
      		</tr>
      		<tr>
      			<td class="cv-label"><xsl:copy-of select="cv:phone/node()"/></td>
      			<td><xsl:apply-templates select="$p/ad:phone[@type = 'business']" mode="cv"/></td>
      			<td class="cv-label"><xsl:apply-templates select="cv:label(cv:citizen)"/></td>
      			<td><xsl:apply-templates select="cv:val(cv:citizen, $p/ad:citizen)"/></td>
      		</tr>
      		<tr>
      			<td class="cv-label"><xsl:copy-of select="cv:email/node()"/></td>
      			<td><xsl:apply-templates select="$p/ad:email[@type = 'business']" mode="cv"/></td>
      			<td class="cv-label"><xsl:apply-templates select="cv:label(cv:sex)"/></td>
      			<td><xsl:apply-templates select="cv:val(cv:sex, $p/ad:sex)"/></td>
      		</tr>
      		<tr>
      			<td class="cv-label">&#160;</td>
      			<td>&#160;</td>
      			<td class="cv-label"><xsl:apply-templates select="cv:label(cv:civil-state)"/></td>
      			<td><xsl:apply-templates select="cv:val(cv:civil-state, $p/ad:civil-state)"/></td>
      		</tr>
      	</tbody>
      </table>
    </div>
  </xsl:template>
  
	<xsl:template match="ad:address[@ref]" mode="#all">
		<xsl:apply-templates select="f:entity(@ref)" mode="#current"/>
	</xsl:template>
	
	<xsl:template match="ad:address" mode="cv">
		<div id="{@id}">
			<xsl:if test="ad:street">
				<xsl:value-of select="ad:street" />&#160;<xsl:value-of select="ad:house"/><br/>
			</xsl:if>
			<xsl:value-of select="upper-case(ad:country)"/>
			<xsl:text>-</xsl:text>
			<xsl:value-of select="ad:zip"/>&#160;<xsl:value-of select="ad:city"/>
		</div>
	</xsl:template>
	
	<xsl:template match="ad:phone" mode="cv">
		<span id="{@id}">
			<xsl:apply-templates select="." mode="format"/>
		</span>
	</xsl:template>
	
	<xsl:template match="ad:email" mode="cv">
		<a id="{@id}" href="#" title="send mail">
			<xsl:attribute name="onclick">
				<xsl:apply-templates select="." mode="onclick"/>
			</xsl:attribute>
			<xsl:apply-templates select="." mode="format"/>
		</a>
	</xsl:template>
	
	<xsl:template match="ad:address" mode="work">
		<xsl:value-of select="ad:city"/>
		<xsl:text>, </xsl:text>
		<xsl:value-of select="upper-case(ad:country)"/>
	</xsl:template>
	
	<xsl:template match="cv:activities">
		<div id="{@id}">
			<v:style ref="{resolve-uri('styles.css')}"/>
			<table class="cv">
				<tbody>
					<xsl:apply-templates select="cv:*"/>
				</tbody>
			</table>
		</div>
	</xsl:template>
	
	<xsl:template match="cv:education">
		<tr id="{@id}">
			<td class="cv-label"><xsl:apply-templates select="cv:periode"/></td>
			<td>
				<xsl:apply-templates select="cv:description"/>
				<xsl:apply-templates select="cv:faculty"/>
			</td>
		</tr>
	</xsl:template>
	
	<xsl:template match="cv:periode">
		<div id="{@id}">
			<!-- xs:date() cannot handle incomplete strings (only year: '2001', only year-month: '2001-05') -->
			<!-- only year -->
			<xsl:variable name="s" select="if (contains(@start, '-')) then substring-before(@start, '-') else @start"/>
			<xsl:variable name="e" select="if (contains(@end, '-')) then substring-before(@end, '-') else @end"/>
			<xsl:value-of select="$s"/>
			<xsl:if test="not(@end) or ($e != $s)">
				<xsl:text> - </xsl:text>
				<xsl:value-of select="$e"/>
				<xsl:if test="not(@end)">...&#160;&#160;&#160;</xsl:if>
			</xsl:if>
		</div>
	</xsl:template>
	
	<xsl:template match="cv:description | cv:label | cv:profession">
		<xsl:apply-templates select="node()"/>
	</xsl:template>
	
	<xsl:template match="cv:faculty">
		<xsl:text>; </xsl:text>
		<xsl:value-of select="."/>
	</xsl:template>
	
	<xsl:template match="cv:work">
		<tr id="{@id}">
			<td class="cv-label"><xsl:apply-templates select="cv:periode"/></td>
			<td>
				<div>
					<xsl:apply-templates select="cv:profession"/>
					<xsl:value-of select="../text[@key = 'at']"/>
					<xsl:apply-templates select="f:entity(@company)"/>
				</div>
				<ul>
					<xsl:apply-templates select="cv:task"/>
				</ul>
			</td>
		</tr>
	</xsl:template>
	
	<xsl:template match="cv:task">
		<li id="{@id}"><xsl:apply-templates select="node()"/></li>
	</xsl:template>
	
	<xsl:template match="cv:skills">
		<div id="{@id}">
			<v:style ref="{resolve-uri('styles.css')}"/>
			<table class="cv">
				<tbody>
					<xsl:apply-templates select="cv:*"/>
				</tbody>
			</table>
		</div>
	</xsl:template>
	
	<xsl:template match="cv:language">
		<tr id="{@id}">
			<td class="cv-label"><xsl:apply-templates select="cv:label"/></td>
			<td><xsl:apply-templates select="cv:description"/></td>
		</tr>
	</xsl:template>
	
	<xsl:template match="cv:interests">
		<div id="{@id}">
			<v:style ref="{resolve-uri('styles.css')}"/>
			<table class="cv">
				<tbody>
					<xsl:apply-templates select="cv:*"/>
				</tbody>
			</table>
		</div>
	</xsl:template>
	
	<xsl:template match="cv:category">
		<tr id="{@id}">
			<td class="cv-label"><xsl:apply-templates select="cv:label"/></td>
			<td><xsl:apply-templates select="cv:* except cv:label"/></td>
		</tr>
	</xsl:template>
	
	<xsl:template match="cv:product">
		<tr id="{@id}">
			<td class="cv-label"><xsl:value-of select="@title"/></td>
			<td><xsl:apply-templates select="node()"/></td>
		</tr>
	</xsl:template>
	
	<xsl:template match="cv:category/cv:interest">
		<xsl:if test="position() > 1">; </xsl:if>
		<xsl:apply-templates select="node()"/>
	</xsl:template>
	
	<xsl:template match="cv:category/cv:product">
		<xsl:if test="position() > 1">, </xsl:if>
		<span id="{@id}">
			<xsl:value-of select="@title"/>
			<xsl:text> </xsl:text>
			<xsl:value-of select="@release"/>
		</span>
	</xsl:template>
	
	<xsl:template match="cv:experiences">
		<div id="{@id}">
			<xsl:apply-templates select="cv:*"/>
		</div>
	</xsl:template>
	
	<xsl:template match="cv:project">
		<v:chapter id="{@id}">
			<v:style ref="{resolve-uri('styles.css')}"/>
			<v:title class="inlineEdit">
				<span class="inlnEd_path" title="title"/>
				<xsl:value-of select="cv:title"/>
			</v:title>
			<xsl:apply-templates select="doc:* | html:*"/>
			<xsl:if test="cv:customer | cv:statistic">
				<table class="cv">
					<tbody>
						<xsl:if test="cv:customer">
							<tr>
								<td class="cv-label"><xsl:value-of select="ancestor::*/text[@key = 'customer'][1]"/></td>
								<td><xsl:apply-templates select="cv:customer"/></td>
							</tr>
						</xsl:if>
						<xsl:if test="cv:statistic">
							<tr>
								<td class="cv-label"><xsl:value-of select="ancestor::*/text[@key = 'statistic'][1]"/></td>
								<td><xsl:apply-templates select="cv:statistic"/></td>
							</tr>
						</xsl:if>
					</tbody>
				</table>
			</xsl:if>
		</v:chapter>
	</xsl:template>
	
	<xsl:template match="cv:customer">
		<xsl:if test="position() > 1">, </xsl:if>
		<span id="{@id}">
			<xsl:value-of select="f:entity(@company)/ad:title"/>
		</span>
	</xsl:template>
	
	<xsl:template match="cv:statistic">
		<xsl:if test="position() > 1">, </xsl:if>
		<span id="{@id}" class="inlineEdit"><xsl:value-of select="."/></span>
	</xsl:template>
	
	<xsl:template match="ad:company">
		<xsl:value-of select="ad:title"/>
		<xsl:text>, </xsl:text>
		<xsl:apply-templates select="ad:address[@type = 'main']" mode="work"/>
	</xsl:template>
	
	<xsl:function name="cv:label">
		<xsl:param name="spec"/>
		<xsl:sequence select="if ($spec/cv:label) then $spec/cv:label/node() else $spec/node()"/>
	</xsl:function>
	
	<xsl:function name="cv:val">
		<xsl:param name="spec"/>
		<xsl:param name="val"/>
		<xsl:variable name="_v" select="$spec/cv:val[@key = $val]"/>
		<xsl:sequence select="if ($_v) then $_v/node() else $val/node()"/>
	</xsl:function>
	
</xsl:stylesheet>
